(() => {

    let ele = {}
    ele.listone = $('#list-one');
    ele.listTwo = $('#list-two');

    ele.options = $('#tab-team li');

    // 去绑定
    ele.modal_bang = $('#modal-bang')
    ele.close_bang = $('#close-bang')
    ele.close_bang.onclick = () => {
        ele.modal_bang.style.display = 'none'
    }

    // 官方QQ聊天群
    ele.modal_rewards = $('#modal-rewards')
    ele.close_reward = $('#close-reward')
    ele.codeni = $('.codeni')
    ele.copy = $('.copy')
    // 装备奖励QQ群
    // ele.equipment.onclick = () => {
    //     ele.modal_rewards.style.display = 'block'
    // }
    ele.close_reward.onclick = () => {
        ele.modal_rewards.style.display = 'none'
    }
    //  复制装备奖励QQ群
    for (let i = 0; i < ele.copy.length; i++) {
        ele.copy[i].onclick = () => {
            let flag = copy(ele.codeni[i].innerHTML);
            if (flag) openTips("复制成功");
            else openTips("复制失败，请手动复制");
        }
    }

    getUserInfo()
    tabBar()
    function tabBar() {
        for (let i = 0; i < ele.options.length; i++) {
            ele.options[i].addEventListener('click', e => {
                let cls = e.target.className;
                typeName = cls.split(' ')[0].split('-')[1];
                console.log(typeName);
                if (typeName == 'one') {
                    getList();
                }
                else if (typeName == 'two') {
                }
                else if (typeName == 'three') {
                    getList2();
                }

            })
        }
    }


    // 领奖规则
    ele.rule = $('#rule');
    ele.modal_rule = $('#modal-rule');
    ele.confirm_rule = $('#confirm-rule');
    // 领奖规则弹框
    ele.rule.onclick = () => {
        ele.modal_rule.style.display = 'block'
    }
    ele.confirm_rule.onclick = () => {
        ele.modal_rule.style.display = 'none'
    }

    // 恭喜您成功领取
    ele.modal_success = $('#modal-success');
    ele.confirm_success = $('#confirm-success');
    ele.successbox2 = $('#successbox2');
    ele.sunum1 = $('#sunum1');
    ele.sunum2 = $('#sunum2');
    // 恭喜您成功领取
    ele.confirm_success.onclick = () => {
        ele.modal_success.style.display = 'none'
    }

    // 有效认证 不可领取 划入式
    ele.modal_nolist = $('#modal-nolist');
    ele.slide_close = $('#slide-close');
    ele.nolistbox = $('#nolistbox');
    //有效认证 不可领取
    ele.slide_close.onclick = () => {
        ele.modal_nolist.style.display = 'none'
    }

    // 仙王认证 不可领取
    ele.modal_Cepheus = $('#modal-Cepheus');
    ele.Cepheus_close = $('#Cepheus-close');
    //仙王认证 不可领取
    ele.Cepheus_close.onclick = () => {
        ele.modal_Cepheus.style.display = 'none'
    }

    // 获取新人任务列表
    getList();
    function getList() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.WELFAREFULI,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: 1
            },
            success: res => {
                if (res.code == 1) drawPropList(res.data.list);
            }
        });
    }
    // 渲染新人任务列表
    function drawPropList(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let status = lv.complete_status ? 'received' : 'receive';
            let txt = lv.complete_status == 1 ? '已领取' : '领取';
            let box = ''
            let p = ''
            let s = ''
            if (lv.reward_2) {
                box = '<div class="cibi">' +
                    '<div class="imgk3">' +
                    '<div class="himgbox3">' +
                    '<img class="himg3" src="http://img.qunx.top/xianwang/public/maodao.png" alt="">' +
                    '</div>' +
                    '</div>' +
                    '<span class="hnum">1米秒到次数*' + lv.reward_2_num + '</span>' +
                    '</div>'
            } else {
                box = ''
            }

            if (lv.id == 1) {
                p = '<p class="level">' + lv.title + '(领取成功后其可前往商店已获得页面开启盲盒)</p>'
            } else if (lv.id == 5) {
                p = '<p class="level">' + lv.title + '(' + lv.invite_immortal + '/3)</p>'
                setCookie('invite_immortal', lv.invite_immortal)
            } else if (lv.id == 4) {
                p = '<p class="level">' + lv.title + '</p>'
                setCookie('t_invite_immortal', lv.t_invite_immortal)
            }
            else {
                p = '<p class="level">' + lv.title + '</p>'
            }

            if (lv.id == 1) {
                s = '<p class="jpp">' + '新人盲盒可在商店-已获得中开出1-8米零钱' + '</p>'
            }
            else if (lv.id == 5) {
                s = '<p class="jpp">' + '每日邀请任务' + '</p>'
            } else {
                s
            }
            let go = ''
            if (lv.id == 1) {
                go = '<button class="gogo" onclick=opent(1)>' +
                    '<span class="salen">' + '去开盲盒' + '</span>' +
                    '</button>'
            }
            else if (lv.id == 2) {
                go = '<button class="gogo" onclick=opent(2)>' +
                    '<span class="salen">' + '点击进群' + '</span>' +
                    '</button>'
            } else {
                go = ''
            }
            html += s +
                '<div class="item">' +
                p +
                '<div class="cbox">' +
                '<div class="word">' +

                '<div class="cibi">' +
                '<div class="imgk3">' +
                '<div class="himgbox3">' +
                '<img class="himg3" src="http://img.qunx.top/xianwang/maghe/1.png" alt="">' +
                '</div>' +
                '</div>' +
                '<span class="hnum">新人盲盒*' + lv.reward_num + '</span>' +
                '</div>' +

                box +

                '</div>' +
                go +
                '<button class="' + status + '" data="' + lv.id + '" >' +
                '<span class="salen receive-' + lv.id + '">' + txt + '</span>' +
                '</button>' +
                '</div>' +
                '</div>'

        }
        ele.listone.innerHTML = html;
        bindReceive();
    }

    window.opent = opent
    function opent(id) {
        if (id == 1) {
            location.href = 'bonus.html'
        } else {
            ele.modal_rewards.style.display = 'block'
        }
    }

    // 绑定新人任务领取按钮
    function bindReceive() {
        let btns = $('.receive');
        for (let i = 0; i < btns.length; i++) {
            btns[i].onclick = function () {
                receiveReward(this.getAttribute('data'));
            }
        }
        let btnds = $('.received');
        for (let i = 0; i < btnds.length; i++) {
            btnds[i].onclick = function () {
                openTips('您已领取过该奖励');
            }
        }
    }
    // 领取新人任务奖励
    function receiveReward(lv) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.WELFAREFULICOLLECT,
            method: 'POST',
            data: {
                task_id: lv,
            },
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    successData(res.data.task)
                    let btn = $('.receive-' + lv)[0];
                    btn.innerHTML = "已领取"
                    btn.classList.remove('receive');
                    btn.classList.add('received');
                    btn.onclick = () => {
                        openTips('您已领取过该奖励');
                    };
                } else {
                    if (lv == 2) {
                        ele.modal_bang.style.display = 'block'
                    } else if (lv == 3 && res.data.need_popup) {
                        getnolist();
                    } else if (lv == 4 && res.data.need_popup) {
                        ele.modal_Cepheus.style.display = 'block'
                    } else {
                        openTips(res.msg);
                    }

                }
            }
        })
    }
    // 渲染成功领取页面
    function successData(data) {
        // console.log(data.reward_num, data.reward_2_num);
        ele.sunum1.innerHTML = data.reward_num
        if (data.reward_2_num) {
            ele.sunum2.innerHTML = data.reward_2_num
            ele.successbox2.style.display = 'block'
        } else {
            ele.successbox2.style.display = 'none'
        }
        ele.modal_success.style.display = 'block'
    }

    // 等级奖励
    function getList2() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.WELFAREFULI,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: 3
            },
            success: res => {
                if (res.code == 1) drawPropList2(res.data.list);
                else openTips(res.msg);
            }
        });
    }
    // 渲染等级奖励列表
    function drawPropList2(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let status = lv.complete_status ? 'releived' : 'releive';
            let txt = lv.complete_status == 1 ? '已领取' : '领取';
            html += '<div class="item">' +
                '<p class="level">' + lv.title + '</p>' +
                '<div class="cbox">' +
                '<div class="word">' +
                '<div class="cibi">' +
                '<div class="imgk">' +
                '<div class="himgbox">' +
                '<img class="himg" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '</div>' +
                '</div>' +
                '<span class="hnum">' + lv.reward_num + '灵石</span>' +
                '</div>' +
                '<div class="cibi">' +
                '<div class="imgk2">' +
                '<div class="himgbox6">' +
                '<img class="himg2" src="http://img.qunx.top/xianwang/public/hongbao.png" alt="">' +
                '</div>' +
                '</div>' +
                '<span class="hnum">' + lv.reward_2_num + '现金红包</span>' +
                '</div>' +
                '</div>' +
                '<button class="' + status + '" data="' + lv.id + '">' +
                '<span class="rebox releive-' + lv.id + '">' + txt + '</span>' +
                '</button>' +
                '</div>' +
                '</div>'
        }
        ele.listTwo.innerHTML = html;
        bindReceive2();
    }
    // 绑定等级奖励领取按钮
    function bindReceive2() {
        let btns = $('.releive');
        for (let i = 0; i < btns.length; i++) {
            btns[i].onclick = function () {
                receiveReward2(this.getAttribute('data'));
            }
        }
        let btnds = $('.releived');
        for (let i = 0; i < btnds.length; i++) {
            btnds[i].onclick = function () {
                openTips('您已领取过该奖励');
            }
        }
    }
    // 领取等级奖励
    function receiveReward2(lv) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.WELFAREFULICOLLECT,
            method: 'POST',
            data: {
                task_id: lv
            },
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    let btn = $('.releive-' + lv)[0];
                    btn.innerHTML = "已领取"
                    btn.classList.remove('releive');
                    btn.classList.add('releived');
                    btn.onclick = () => {
                        openTips('您已领取过该奖励');
                    };
                    openTips(res.msg);
                } else {
                    openTips(res.msg);
                }
            }
        })
    }



    const Effective = [
        {
            name: '完成实名认证',
            des: '绑定支付宝即可免费完成实名认证',
            is_receive: 0
        },
        {
            name: '绑定游戏区服和角色',
            des: '下载游戏并创建角色后，<br>在“我的”界面头像右侧点击绑定',
            is_receive: 0
        },
        {
            name: '游戏等级达到90级',
            des: '游戏简单易上手，轻松升到90级',
            is_receive: 0
        },
    ]

    // 获取有效认证不可领取列表
    function getnolist() {
        getUserInfo(res => {
            for (let i = 0; i < Effective.length; i++) {
                Effective[0].is_receive = res.is_check
                Effective[1].is_receive = res.server_name ? 1 : 0
                Effective[2].is_receive = res.game_level >= 90 ? 1 : 0
            }
            NodrawPropList(Effective)
        })
    }

    // 渲染有效认证奖励列表
    function NodrawPropList(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let status = lv.is_receive ? 'Noreceived' : 'Noreceive';
            // let txt = lv.is_receive == 0 ? '去完成' : '已完成';
            let txt = ''
            if (lv.is_receive == 0 && i == 0) {
                txt = '去完成'
            } else if (lv.is_receive == 0 && i == 1) {
                txt = '去绑定'
            } else if (lv.is_receive == 0 && i == 2) {
                txt = '去完成'
            } else {
                txt = '已完成'
            }
            let go = ''
            if (i == 0) {
                go = "location.href='realname.html'"
            } else if (i == 1) {
                go = "location.href='personDetail.html'"
            } else {
                go = ''
            }
            html += '<div class="nobpx">' +
                '<div class="noleft">' +
                '<p class="n1">' + lv.name + '</p>' +
                '<p class="n2">' + lv.des + '</p>' +
                '</div>' +
                '<button class="' + status + '" onclick=' + go + ' >' +
                '<span  class="rebox  Noreceive-' + lv.level + '">' + txt + '</span>' +
                '</button>' +
                '</div>';

        }
        ele.nolistbox.innerHTML = html;
        ele.modal_nolist.style.display = 'block'
    }

})();